import { smsLogMock } from './data/log'
import { isDateTimeInRange } from '../../../utils'
let smsLogList = smsLogMock(400)
export default [
  {
    url: RegExp('/system/sms-log/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  },
  {
    url: RegExp('/system/sms-log/export' + '.*'), // 导出
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByExport(params)
      }
    }
  }
]

const getListByPage = params => {
  let result = smsLogList
  result = result.filter(item => !item.deleted)
  if (params.query.mobile) {
    result = result.filter(item => item.mobile.indexOf(params.query.mobile) !== -1)
  }
  if (params.query.channelId) {
    result = result.filter(item => item.channelId === parseInt(params.query.channelId))
  }
  if (params.query.templateId) {
    result = result.filter(item => item.templateId === parseInt(params.query.templateId))
  }
  if (params.query.sendStatus) {
    result = result.filter(item => item.sendStatus === parseInt(params.query.sendStatus))
  }
  if (params.query.sendTime && params.query.sendTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.sendTime[0], params.query.sendTime[1], item.sendTime))
  }
  if (params.query.receiveStatus) {
    result = result.filter(item => item.receiveStatus === parseInt(params.query.receiveStatus))
  }
  if (params.query.receiveTime && params.query.receiveTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.receiveTime[0], params.query.receiveTime[1], item.receiveTime))
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const getListByExport = params => {
  let result = smsLogList
  result = result.filter(item => !item.deleted)
  if (params.query.mobile) {
    result = result.filter(item => item.mobile.indexOf(params.query.mobile) !== -1)
  }
  if (params.query.channelId) {
    result = result.filter(item => item.channelId === parseInt(params.query.channelId))
  }
  if (params.query.templateId) {
    result = result.filter(item => item.templateId === parseInt(params.query.templateId))
  }
  if (params.query.sendStatus) {
    result = result.filter(item => item.sendStatus === parseInt(params.query.sendStatus))
  }
  if (params.query.sendTime && params.query.sendTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.sendTime[0], params.query.sendTime[1], item.sendTime))
  }
  if (params.query.receiveStatus) {
    result = result.filter(item => item.receiveStatus === parseInt(params.query.receiveStatus))
  }
  if (params.query.receiveTime && params.query.receiveTime.length !== 0) {
    result = result.filter(item => isDateTimeInRange(params.query.receiveTime[0], params.query.receiveTime[1], item.receiveTime))
  }
  return result
}
